package com.bluefox.modules.quartz.config;

import com.bluefox.modules.quartz.domain.QuartzJob;
import com.bluefox.modules.quartz.repository.QuartzJobRepository;
import com.bluefox.modules.quartz.utils.QuartzManage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName JobRunner
 * @Description JobRunner
 * @Author SunFeng
 * @Date 2020/2/28 15:29
 * @Version 1.0
 */
@Slf4j
@Component
public class JobRunner implements ApplicationRunner {
    private final QuartzJobRepository quartzJobRepository;

    private final QuartzManage quartzManage;

    public JobRunner(QuartzJobRepository quartzJobRepository, QuartzManage quartzManage) {
        this.quartzJobRepository = quartzJobRepository;
        this.quartzManage = quartzManage;
    }

    /**
     * 项目启动时重新激活启用的定时任务
     * @param applicationArguments /
     */
    @Override
    public void run(ApplicationArguments applicationArguments){

        List<QuartzJob> quartzJobs = quartzJobRepository.findByIsPauseIsFalse();
        quartzJobs.forEach(quartzManage::addJob);
        log.info("--------------------定时任务注入完成---------------------");
    }
}
